<!doctype html>
<html>
  <head>
    <!-- MathJax -->
    <script type="text/javascript"
      src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
    </script>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <title>
      Caffe | CaffeNet C++ Classification example
    </title>

    <link rel="icon" type="image/png" href="/images/caffeine-icon.png">

    <link rel="stylesheet" href="/stylesheets/reset.css">
    <link rel="stylesheet" href="/stylesheets/styles.css">
    <link rel="stylesheet" href="/stylesheets/pygment_trac.css">

    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <!--[if lt IE 9]>
    <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body>
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-46255508-1', 'daggerfs.com');
    ga('send', 'pageview');
  </script>
    <div class="wrapper">
      <header>
        <h1 class="header"><a href="/">Caffe</a></h1>
        <p class="header">
          Deep learning framework by <a class="header name" href="http://bair.berkeley.edu/">BAIR</a>
        </p>
        <p class="header">
          Created by
          <br>
          <a class="header name" href="http://daggerfs.com/">Yangqing Jia</a>
          <br>
          Lead Developer
          <br>
          <a class="header name" href="http://imaginarynumber.net/">Evan Shelhamer</a>
        <ul>
          <li>
            <a class="buttons github" href="https://github.com/BVLC/caffe">View On GitHub</a>
          </li>
        </ul>
      </header>
      <section>

      <h1 id="classifying-imagenet-using-the-c-api">Classifying ImageNet: using the C++ API</h1>

<p>Caffe, at its core, is written in C++. It is possible to use the C++
API of Caffe to implement an image classification application similar
to the Python code presented in one of the Notebook examples. To look
at a more general-purpose example of the Caffe C++ API, you should
study the source code of the command line tool <code class="highlighter-rouge">caffe</code> in <code class="highlighter-rouge">tools/caffe.cpp</code>.</p>

<h2 id="presentation">Presentation</h2>

<p>A simple C++ code is proposed in
<code class="highlighter-rouge">examples/cpp_classification/classification.cpp</code>. For the sake of
simplicity, this example does not support oversampling of a single
sample nor batching of multiple independent samples. This example is
not trying to reach the maximum possible classification throughput on
a system, but special care was given to avoid unnecessary
pessimization while keeping the code readable.</p>

<h2 id="compiling">Compiling</h2>

<p>The C++ example is built automatically when compiling Caffe. To
compile Caffe you should follow the documented instructions. The
classification example will be built as <code class="highlighter-rouge">examples/classification.bin</code>
in your build directory.</p>

<h2 id="usage">Usage</h2>

<p>To use the pre-trained CaffeNet model with the classification example,
you need to download it from the “Model Zoo” using the following
script:</p>
<div class="highlighter-rouge"><pre class="highlight"><code>./scripts/download_model_binary.py models/bvlc_reference_caffenet
</code></pre>
</div>
<p>The ImageNet labels file (also called the <em>synset file</em>) is also
required in order to map a prediction to the name of the class:</p>
<div class="highlighter-rouge"><pre class="highlight"><code>./data/ilsvrc12/get_ilsvrc_aux.sh
</code></pre>
</div>
<p>Using the files that were downloaded, we can classify the provided cat
image (<code class="highlighter-rouge">examples/images/cat.jpg</code>) using this command:</p>
<div class="highlighter-rouge"><pre class="highlight"><code>./build/examples/cpp_classification/classification.bin \
  models/bvlc_reference_caffenet/deploy.prototxt \
  models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel \
  data/ilsvrc12/imagenet_mean.binaryproto \
  data/ilsvrc12/synset_words.txt \
  examples/images/cat.jpg
</code></pre>
</div>
<p>The output should look like this:</p>
<div class="highlighter-rouge"><pre class="highlight"><code>---------- Prediction for examples/images/cat.jpg ----------
0.3134 - "n02123045 tabby, tabby cat"
0.2380 - "n02123159 tiger cat"
0.1235 - "n02124075 Egyptian cat"
0.1003 - "n02119022 red fox, Vulpes vulpes"
0.0715 - "n02127052 lynx, catamount"
</code></pre>
</div>

<h2 id="improving-performance">Improving Performance</h2>

<p>To further improve performance, you will need to leverage the GPU
more, here are some guidelines:</p>

<ul>
  <li>Move the data on the GPU early and perform all preprocessing
operations there.</li>
  <li>If you have many images to classify simultaneously, you should use
batching (independent images are classified in a single forward pass).</li>
  <li>Use multiple classification threads to ensure the GPU is always fully
utilized and not waiting for an I/O blocked CPU thread.</li>
</ul>


      </section>
  </div>
  </body>
</html>
